package com.web.test.dao;

import com.web.test.bean.EmpInformation;
import com.web.test.utils.JDBCUtil;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by St01 on 2017-9-21.
 */
public class EmpInfoDao {

    public List<EmpInformation> find(String sql)
            throws ClassNotFoundException, SQLException, IOException {

        Connection connection = JDBCUtil.getConnection();
        Statement statement = connection.createStatement();
        ResultSet rSet = statement.executeQuery(sql);

        List<EmpInformation> empInformations = getEmpInfoByRS(rSet);

        JDBCUtil.close(connection, statement, rSet);

        return empInformations;

    }

    private List<EmpInformation> getEmpInfoByRS(ResultSet rSet) throws SQLException {

        List<EmpInformation> msList = new ArrayList<EmpInformation>();

        while (rSet.next()){

            String id = rSet.getString("empno");
            String time = rSet.getString("entry_time");
            String sal = rSet.getString("sal");

            EmpInformation empInformation = new EmpInformation(id,time,sal);
            msList.add(empInformation);
        }
        return msList;
    }

}
